﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using CE.Provider;
using CE.Global;

public partial class Provider_CarInsurance_ProviderCarPointsOnPremium : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCIPREPNT";
    public static Int64 _providerCarPremiumid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!IsPostBack)
        {
            _providerCarPremiumid = 0;
            usrEffectivityDate.StartYear = CE.Global.General.GetUTCDateTime().Year;
            usrEffectivityDate.EndYear = CE.Global.General.GetUTCDateTime().Year + 2;

            if (Request.Params["CPID"] != null)
            {
                Int32 _CarPointsonPremiumid;
                if (Int32.TryParse(Request.Params["CPID"].ToString(), out _CarPointsonPremiumid))
                {
                    GetCarPremium(_CarPointsonPremiumid);
                }
            }
        }
    }
    
    protected void ibtSubmit_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {
                if (_providerCarPremiumid == 0 && CarPointExist())
                {
                    lblMessage.Text = "Point exists.";
                }
                else
                {
                    CarPointsonPremium _CarPointsonPremium = new CarPointsonPremium();
                    CarPointsonPremiumDB _CarPointsonPremiumdb = new CarPointsonPremiumDB();

                    _CarPointsonPremium.ProviderCarPointsId = _providerCarPremiumid;
                    int _trantype = 1;
                    if (_CarPointsonPremium.ProviderCarPointsId > 0)
                    {
                        _trantype = 2;
                    }
                    _CarPointsonPremium.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]);
                    _CarPointsonPremium.Points = Convert.ToInt32(txtPoints.Text.Trim());
                    _CarPointsonPremium.Premium = float.Parse(txtPremium.Text);
                    _CarPointsonPremium.Status = chkStatus.Checked; ;
                    _CarPointsonPremium.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId]);

                    if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
                    {
                        _CarPointsonPremium.FInfo1 = float.Parse(txtNewRate.Text);
                        _CarPointsonPremium.DInfo1 = usrEffectivityDate.GetDate();

                    }
                    else
                    {
                        _CarPointsonPremium.FInfo1 = float.Parse(txtPoints.Text);
                        _CarPointsonPremium.DInfo1 = CE.Global.General.GetUTCDateTime();

                    }

                    _CarPointsonPremium.ProviderCarPointsId = _CarPointsonPremiumdb.Insert(_CarPointsonPremium);

                    if (_CarPointsonPremium.ProviderCarPointsId > 0)
                    {
                        InsertProviderAuthTransaction(_CarPointsonPremium.ProviderCarPointsId, "CICarPointsOnPremium", _trantype, 100, _authcode, _CarPointsonPremium.ProviderCompanyId, _CarPointsonPremium.RefCEUserId);
                        ProviderUpdateSave(_CarPointsonPremium.ProviderCarPointsId);
                        lblMessage.Text = "Saved successfully!";
                        //Response.Redirect("ViewProviderCarPointsonPremium.aspx");
                    }
                    else
                    {
                        lblMessage.Text = "Saving failed!";
                    }
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }

        }
    }

    private void ProviderUpdateSave(Int64 ProviderRefId)
    {
        ProviderUpdates _providerupdates = new ProviderUpdates();
        ProviderUpdatesDB _providerupdatesDB = new ProviderUpdatesDB();

        _providerupdates.UpdateId = 0;
        _providerupdates.CXTableId = GetCXTableId();
        _providerupdates.ProviderRefId = ProviderRefId;

        if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
        {
            //Scheduled
            _providerupdates.UpdateType = 1;
        }
        else
        {
            //Immediate
            _providerupdates.UpdateType = 0;
        }

        _providerupdates.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]);
        _providerupdates.RefCEUserID = Convert.ToInt64(Session[General.UserId].ToString());

        _providerupdatesDB.Insert(_providerupdates);

    }

    private Int32 GetCXTableId()
    {
        Int32 CXID = 0;

        ProviderTableListings _tablelist = new ProviderTableListings();
        ProviderTableListingsDB _tablelistdb = new ProviderTableListingsDB();

        int ProviderType = 100; //Car Insurance
        string CXTable = "tblProviderCarPointsonPremium";

        _tablelist = _tablelistdb.GetProviderTableListingsSingleItem(ProviderType, Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), CXTable);

        if (_tablelist != null)
            CXID = _tablelist.CXTableId;

        return CXID;

    }

    private void GetCarPremium(Int64 CarPointId)
    {
        CarPointsonPremium _CarPointsonPremium = new CarPointsonPremium();
        CarPointsonPremiumDB _CarPointsonPremiumdb = new CarPointsonPremiumDB();

        _CarPointsonPremium = _CarPointsonPremiumdb.GetProviderCarPointsonPremium(CarPointId);

        if (_CarPointsonPremium != null)
        {
            _providerCarPremiumid = Convert.ToInt64(_CarPointsonPremium.ProviderCarPointsId.ToString());
            txtPoints.Text = _CarPointsonPremium.Points.ToString();
            txtPremium.Text = _CarPointsonPremium.Premium.ToString();
            chkStatus.Checked = _CarPointsonPremium.Status;

            if (_CarPointsonPremium.DInfo1 > CE.Global.General.GetUTCDateTime())
            {
                divNewRate.Visible = true;
                txtNewRate.Text = _CarPointsonPremium.FInfo1.ToString();
                rdoEffectivity.SelectedValue = "Yes";
            }
            else
            {
                divNewRate.Visible = false;
                rdoEffectivity.SelectedValue = "No";
            }

            usrEffectivityDate.Day = _CarPointsonPremium.DInfo1.Day;
            usrEffectivityDate.Month = _CarPointsonPremium.DInfo1.Month;
            usrEffectivityDate.Year = _CarPointsonPremium.DInfo1.Year;


        }


    }

    private bool CarPointExist()
    {
        //Clear();
        bool isExist = false;

        CarPointsonPremium _CarPointsonPremium = new CarPointsonPremium();
        CarPointsonPremiumDB _CarPointsonPremiumdb = new CarPointsonPremiumDB();

        _CarPointsonPremium = _CarPointsonPremiumdb.GetProviderCarPointsonPremium(Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), Convert.ToInt32(txtPoints.Text));

        if (_CarPointsonPremium != null)
        {
            isExist = true;
            _providerCarPremiumid = Convert.ToInt64(_CarPointsonPremium.ProviderCarPointsId.ToString());
            txtPoints.Text = _CarPointsonPremium.Points.ToString();
            txtPremium.Text = _CarPointsonPremium.Premium.ToString();
            chkStatus.Checked = _CarPointsonPremium.Status;
        }

        return isExist;

    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ViewProviderCarPointsonPremium.aspx");
    }

    private void Clear()
    {
        _providerCarPremiumid = 0;
        txtPoints.Text = "";
        txtPremium.Text = "";
        chkStatus.Checked = false;
    }

    public void EffectiveWhen(object sender, EventArgs e)
    {
        if (rdoEffectivity.SelectedValue == "Yes")
        {
            divNewRate.Visible = true;
        }
        else
        {
            divNewRate.Visible = false;
        }

        usrEffectivityDate.Day = CE.Global.General.GetUTCDateTime().Day;
        usrEffectivityDate.Month = CE.Global.General.GetUTCDateTime().Month;
        usrEffectivityDate.Year = CE.Global.General.GetUTCDateTime().Year;
    }
    
    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
            //ib.Enabled = false;
            //btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            ibtSubmit.Enabled = true;
            //btnNew.Enabled = true;
            //btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            ibtSubmit.Enabled = true;
            //btnNew.Enabled = true;
            //btnDelete.Enabled = true;
        }
    }

}
